home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 60 / IOPROG_60.ISO / soft / c++ / gsl-1.1.1-setup.exe / {app} / src / combination / test.c < prev   
Encoding:
C/C++ Source or Header  |  2002-01-26  |  3.9 KB  |  149 lines

  1. /* combination/test.c
  2.  * based on permutation/test.c by Brian Gough
  3.  * 
  4.  * Copyright (C) 2001 Szymon Jaroszewicz
  5.  * 
  6.  * This program is free software; you can redistribute it and/or modify
  7.  * it under the terms of the GNU General Public License as published by
  8.  * the Free Software Foundation; either version 2 of the License, or (at
  9.  * your option) any later version.
  10.  * 
  11.  * This program is distributed in the hope that it will be useful, but
  12.  * WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14.  * General Public License for more details.
  15.  * 
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program; if not, write to the Free Software
  18.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  */
  20.  
  21. #include <config.h>
  22. #include <stdlib.h>
  23. #include <stdio.h>
  24. #include <math.h>
  25. #include <gsl/gsl_combination.h>
  26. #include <gsl/gsl_test.h>
  27.  
  28. size_t c63[20][3] = {
  29.   { 0, 1, 2 },  { 0, 1, 3 },  { 0, 1, 4 },  { 0, 1, 5 },
  30.   { 0, 2, 3 },  { 0, 2, 4 },  { 0, 2, 5 },  { 0, 3, 4 },
  31.   { 0, 3, 5 },  { 0, 4, 5 },  { 1, 2, 3 },  { 1, 2, 4 },
  32.   { 1, 2, 5 },  { 1, 3, 4 },  { 1, 3, 5 },  { 1, 4, 5 },
  33.   { 2, 3, 4 },  { 2, 3, 5 },  { 2, 4, 5 },  { 3, 4, 5 }
  34. } ;
  35.  
  36.  
  37. int 
  38. main (void)
  39. {
  40.   size_t i, j;
  41.   int status = 0;
  42.   gsl_combination * c ;
  43.  
  44.   c = gsl_combination_alloc (6,3);
  45.  
  46.   gsl_combination_init_first (c);
  47.   
  48.   i = 0;
  49.  
  50.   do 
  51.     {
  52.       if ( i >= 20 )
  53.         {
  54.       status = 1;
  55.           break;
  56.     }
  57.       for (j = 0; j < 3; j++)
  58.         {
  59.           status |= (c->data[j] != c63[i][j]);
  60.         }
  61.       i++;
  62.     }
  63.   while (gsl_combination_next(c) == GSL_SUCCESS);
  64.  
  65.   gsl_test(status, "gsl_combination_next, 6 choose 3 combination, 20 steps");
  66.  
  67.   gsl_combination_next(c);
  68.   gsl_combination_next(c);
  69.   gsl_combination_next(c);
  70.   for (j = 0; j < 3; j++)
  71.     {
  72.       status |= (c->data[j] != c63[19][j]);
  73.     }
  74.   gsl_test(status, "gsl_combination_next on the last combination");
  75.  
  76.  
  77.   gsl_combination_init_last (c);
  78.  
  79.   i = 20;
  80.   do 
  81.     {
  82.       if ( i == 0 )
  83.         {
  84.       status = 1;
  85.           break;
  86.     }
  87.  
  88.       i--;
  89.  
  90.       for (j = 0; j < 3; j++)
  91.         {
  92.           status |= (c->data[j] != c63[i][j]);
  93.         }
  94.     }
  95.   while (gsl_combination_prev(c) == GSL_SUCCESS);
  96.  
  97.   gsl_test(status, "gsl_combination_prev, 6 choose 3 combination, 20 steps");
  98.  
  99.   gsl_combination_prev(c);
  100.   gsl_combination_prev(c);
  101.   gsl_combination_prev(c);
  102.   for (j = 0; j < 3; j++)
  103.     {
  104.       status |= (c->data[j] != c63[0][j]);
  105.     }
  106.   gsl_test(status, "gsl_combination_prev on the first combination");
  107.   gsl_combination_free (c);
  108.  
  109.   c = gsl_combination_calloc(7, 0);
  110.   /* should return GSL_FAILURE every time */
  111.   status |= (gsl_combination_next(c) != GSL_FAILURE);
  112.   status |= (gsl_combination_next(c) != GSL_FAILURE);
  113.   status |= (gsl_combination_prev(c) != GSL_FAILURE);
  114.   status |= (gsl_combination_prev(c) != GSL_FAILURE);
  115.   gsl_test(status, "gsl_combination 7 choose 0");
  116.   gsl_combination_free (c);
  117.  
  118.   c = gsl_combination_calloc(7, 7);
  119.   /* should return GSL_FAILURE every time */
  120.   for(j = 0; j < 7; j++)
  121.   {
  122.     status |= (gsl_combination_get(c, j) != j);
  123.   }
  124.   status |= (gsl_combination_next(c) != GSL_FAILURE);
  125.   for(j = 0; j < 7; j++)
  126.   {
  127.     status |= (gsl_combination_get(c, j) != j);
  128.   }
  129.   status |= (gsl_combination_next(c) != GSL_FAILURE);
  130.   for(j = 0; j < 7; j++)
  131.   {
  132.     status |= (gsl_combination_get(c, j) != j);
  133.   }
  134.   status |= (gsl_combination_prev(c) != GSL_FAILURE);
  135.   for(j = 0; j < 7; j++)
  136.   {
  137.     status |= (gsl_combination_get(c, j) != j);
  138.   }
  139.   status |= (gsl_combination_prev(c) != GSL_FAILURE);
  140.   for(j = 0; j < 7; j++)
  141.   {
  142.     status |= (gsl_combination_get(c, j) != j);
  143.   }
  144.   gsl_test(status, "gsl_combination 7 choose 7");
  145.   gsl_combination_free (c);
  146.  
  147.   exit (gsl_test_summary());
  148. }
  149.